(:
   A few utilitaries functions
 :)
module namespace jj = "http://kumquat.emse.fr/utilitaires" ;
(:
  Extends a (1 or 2 chars) string to 2 chars if necessary
:)
declare function jj:sl2($s as xs:string) as xs:string
{
  if (fn:string-length($s) = 1)
  then
     fn:concat("0",$s)
  else
     $s
};

(:
  Edit a dateTime value in a human readable form
 :)
declare function jj:date-to-millis($da as xs:dateTime) as xs:integer
{
	let $d := $da - xs:dateTime("1970-01-01T00:00:00")
	return
	xs:integer(1000*(fn:seconds-from-duration($d) + 60 * (fn:minutes-from-duration($d) + 60 *
	(fn:hours-from-duration($d) + 24 * fn:days-from-duration($d)))))
};

(:
  Edit a dateTime value in a human readable form
 :)
declare function jj:editDate($da as xs:dateTime) as xs:string
{
  fn:concat(jj:sl2(xs:string(fn:day-from-dateTime($da))), "/",
            jj:sl2(xs:string(fn:month-from-dateTime($da))), "/",
            fn:year-from-dateTime($da), " ",
            jj:sl2(xs:string(fn:hours-from-dateTime($da))), "h",
            jj:sl2(xs:string(fn:minutes-from-dateTime($da))), ":",
            jj:sl2(xs:string(fn:seconds-from-dateTime($da))))
};

(:
  Edit a dateTime value to only the date part
 :)
declare function jj:formatDDMMYYYY($da as xs:dateTime) as xs:string
{
  fn:concat(jj:sl2(xs:string(fn:day-from-dateTime($da))), "/",
            jj:sl2(xs:string(fn:month-from-dateTime($da))), "/",
            fn:year-from-dateTime($da))
};

(:
  Format milliseconds to value in minutes and seconds
 :)
declare function jj:formatMMMSS($millis as xs:integer) as xs:string
{
  let $secs := ($millis idiv 1000) mod 60
  let $mins := ($millis idiv 1000) idiv 60
  let $minval := if ($mins gt 0) then fn:concat(xs:string($mins),"mn") else ""
  let $secval := fn:concat(jj:sl2(xs:string($secs)),"s")
  return fn:concat($minval,$secval)
};

(:
  Convert an integer representing a Unix date
  (expressed in ms since 1/1/1970)
  to a a human readable form
:)
declare function jj:dateFromDREW($da as node()*) as xs:string
{
  if (fn:empty($da)) then "" else
  let $s := string($da[1])
  return
  if (string-length($s) = 0)
  then
    ""
  else
    concat(jj:editDate(xs:dateTime("1970-01-01T00:00:00")+xs:integer($s) idiv 1000),
         ".",jj:sl2(string((xs:integer($s)+50) idiv 10 mod 100)))
};

(:
  Convert an integer representing a Unix date
  (expressed in ms since 1/1/1970)
  to a a dateTime
:)
declare function jj:unix-to-dateTime($s as xs:integer) as xs:dateTime
{
  xs:dateTime("1970-01-01T00:00:00") + $s idiv 1000
};


(:
   Generate a "path" expression for an element
 :)
declare function jj:build-Path($elt as element()) as xs:string
{
   if (fn:empty($elt/..))
   then
      ""
   else
      let $n := fn:local-name($elt)
      for $i at $j in $elt/../*[fn:local-name(.)=$n]
      where $i is $elt
      return fn:concat(jj:build-Path($elt/..), "/", $n, "[", $j, "]")
      
};

(: Get an info from an item :)
declare function jj:get-item-info($elt as element(), $id as xs:string) as node()*
{
   $elt/info[@name=$id]/node()
}

(: Get an info from an item :)
declare function jj:get-info($elt as element(), $id as xs:string) as node()*
{
   jj:get-item-info($elt,$id)
}

(: Get the first value or the second if the first is empty :)
declare function jj:get-one($elt1 as item()*, $elt2 as item()*) as item()*
{
   if (fn:empty($elt1)) then $elt2 else $elt1
}

declare function jj:get-text($infonode as node()*) as xs:string
{
	 if (fn:empty($infonode)) then
	 		""
	  else
	  	let $ret := jj:get-one($infonode/text/text(),$infonode/text())
	  	return 
	  	if (fn:empty($ret)) then "" else $ret
}

(:doesn't works?!!!!:)
declare function jj:get-info-text($elt as element(), $id as xs:string) as xs:string
{
	jj:get-text(jj:get-info($elt,$id))
}

declare function jj:prod($arg as xs:anyAtomicType*, $one as xs:anyAtomicType) as xs:anyAtomicType
{
	if (fn:empty($arg)) then $one
	else $arg[1] * jj:prod($arg[position() > 1], $one)
}

declare function jj:remove-html($s as xs:string*) as xs:string
{
if (fn:empty($s)) then "" else
let $s1 := replace($s, "&lt;BR.?/?&gt;", "
","i")
let $s2 := replace($s1, "<blockquote.*?>", "
")
let $s3 := replace($s2, "</blockquote>", "
")
let $s4 := replace($s3, "<a.*?>", "<a>")
let $s5 := replace($s4, "<img.*?>", "<img>")
let $s6 := replace($s5, "<div.*?>", "
")
let $s7 := replace($s6, "</a>", "")
let $s8 := replace($s7, "</div>", "
")
let $s9 := replace($s8, "</span>", "")
let $s10 := replace($s9, "<span.*?>", "")
let $s11 := replace($s10, "<p.*?>", "
")
let $sl := replace($s11, "</p>", "
")
return $sl
}